require('PhoenixNirvana');
const
	colors = require('colors'),
	fs = require('fs'),
	request = require('superagent');
	
require('superagent-proxy')(request);
require('superagent-charset')(request);

let tmpl = new nirvana.Template(fs.readFileSync('foo.html').toString());
let res = tmpl.render({username: 'hang'});

let [m,h,b] = nirvana.util.parseHttpPacket(res);
//console.log(m, h, b);

// let agent = request.agent();  // 保存cookie状态
// agent.get(...)
let uri = m.path;
if (!/^http/i.test(m.path)) uri = h['Host'] + uri;
let req = request(m.method, uri)
	.set(h)
	//.proxy('http://127.0.0.1:8888')
	
if (/post/i.test(m.method))
	req.send(b);

req
	//.connect("127.0.0.1")
	/*.connect({
		"redir.example.com": "127.0.0.1", // redir.example.com:555 will use 127.0.0.1:555
		"www.example.com": false, // don't override this one; use DNS as normal
		"*": "proxy.example.com", // all other requests will go to this host
	})*/
	//.retry(2)
	//.auth('user', 'byhgj')
	//.attach('image1', 'path/to/1.jpg')    // filename=image1, ...
	.then(res => {
		console.log('res');
	})
